package com.osard.screen.utils;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.osard.screen.activity.ScreenRecordActivity;
import com.osard.screen.enumerate.ScreenRecordDirectionEnum;
import com.osard.screen.event.ScreenRecordSuccessEvent;
import com.osard.screen.service.ScreenRecordService;

import java.io.File;
import java.util.List;

/**
 * 开始录制时未设置路径，则默认放置“手机存储-Android-data-包名-Cache-Movies”下
 * 文件名默认为时间戳
 * <p>
 * 作者：MJSoftKing
 */
public class ScreenRecordUtils {

    private static final String TAG = ScreenRecordUtils.class.getName();
    private final static ScreenRecordUtils SCREEN_RECORD = new ScreenRecordUtils();

    private static boolean isInit = false;
    //application对象
    private static Application context;
    private Class<?> serviceName;

    private ScreenRecordUtils() {
    }

    /**
     * 使用自身service初始化视频录制
     *
     * @param application app
     */
    public static void init(Application application) {
        init(application, ScreenRecordService.class);
    }

    /**
     * 使用继承ScreenRecordService重写前台通知栏的服务初始化
     * 多次初始化时，以第一次为准
     *
     * @param application APP
     * @param serviceName 继承ScreenRecordService的服务
     */
    public static void init(Application application, Class<? extends ScreenRecordService> serviceName) {
        if (!isInit) {
            context = application;

            SCREEN_RECORD.setServiceName(serviceName);
        }
    }

    void setServiceName(Class<?> serviceName) {
        this.serviceName = serviceName;
    }

    Class<?> getServiceName() {
        return serviceName;
    }

    /**
     * 使用竖屏或横屏开始录制。
     *
     * @param videoPath 视频存放全路径，例如：“/storage/emulated/0/Android/data/包名/cache/Movies/”
     * @param videoName 视频名称，后缀为”.mp4“。
     * @param direction 录制屏幕方向
     */
    public static boolean start(@Nullable String videoPath,
                                @Nullable String videoName,
                                @Nullable ScreenRecordDirectionEnum direction) {
        if (!isRunService()) {
            Intent intent = new Intent();
            intent.setClass(context, ScreenRecordActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(ScreenRecordActivity.REQUESTED_ORIENTATION, direction);
            intent.putExtra(ScreenRecordActivity.TARGET_SERVICE, SCREEN_RECORD.getServiceName());

            Bundle bundle = new Bundle();
            bundle.putString(ScreenRecordService.VIDEO_PATH, videoPath);
            bundle.putString(ScreenRecordService.VIDEO_NAME, videoName);
            intent.putExtras(bundle);

            context.startActivity(intent);
            return true;
        } else {
            //已在运行，不能再次启动
            return false;
        }
    }

    /**
     * 使用竖屏或横屏开始录制。
     *
     * @param videoPath 视频存放全路径，例如：“/storage/emulated/0/Android/data/包名/cache/Movies/”
     * @param videoName 视频名称，后缀为”.mp4“。
     */
    public static boolean start(@Nullable String videoPath,
                                @Nullable String videoName) {
        return start(videoPath, videoName, null);
    }

    /**
     * 使用竖屏或横屏开始录制。
     *
     * @param direction 录制屏幕方向
     */
    public static boolean start(@Nullable ScreenRecordDirectionEnum direction) {
        return start(null, null, direction);
    }

    /**
     * 使用调用时的屏幕方向开始录制
     */
    public static boolean start() {
        return start(null);
    }

    /**
     * 停止录制，成功录制时，触发{@link ScreenRecordSuccessEvent} 事件
     */
    public static boolean stop() {
        if (isRunService()) {
            context.stopService(new Intent(context, SCREEN_RECORD.getServiceName()));
            return true;
        } else {
            //服务不在运行，直接返回
            return true;
        }
    }

    /**
     * 判断服务是否在运行
     */
    static boolean isRunService() {
        ActivityManager myAM = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(Integer.MAX_VALUE);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            if (SCREEN_RECORD.getServiceName().getName().equals(myList.get(i).service.getClassName())) {
                return true;
            }
        }
        return false;

    }

}
